<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>
<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="true"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="true"></uieip>
    <title>Data Edit</title>
    <style>
        body {
            margin: 0;
        }

        table {
            border-spacing: 0px;
        }
    </style>
</head>

<body>
    <div id="div_layout" class="easyui-layout" style="width:100%;height:750px;">
        <div data-options="region:'west',split:true" style="width:260px;">
            <div style="margin:3px 0 0 3px">
                <select id="cg_cms" style="width: 300px;"></select>
            </div>
            <div id="tt_navigation" style="overflow: auto; border: 1px solid rgb(239, 239, 239); margin: 3px 0 0 0;">
            </div>
        </div>
        <div data-options="region:'center',border:false" style="padding: 0 0 0 20px;">
            <h3>站点信息</h3>
            <div id="tt_site_info"></div>
            <div style="padding-top: 150px;">
                <h3>导航信息</h3>
                <div id="tt_navigation_info"></div>
            </div>
        </div>
    </div>
    <script>
        var dbname = "yqbase";

        var setResizeTime;
        function form_load() {
            $(window).resize(function () {
                onresize();
            });
            onresize();

            var $west = $("#div_layout").layout("panel", "west");
            $west.panel("options").onResize = function (width, height) {
                if (setResizeTime === undefined) {
                    setResizeTime = setTimeout(function () {
                        console.log("sssss");
                        onresize();
                    }, 100);
                }
            }

            $("#cg_cms").yqComboGrid("loadData");
        }

        function onresize() {
            $("#div_layout").layout("resize", {
                width: $(window).width(),
                height: $(window).height()
            });
            var $west = $("#div_layout").layout("panel", "west");

            $('#cg_cms').yqComboGrid("resize", {
                width: $west.width() - 6
            });

            $("#tt_navigation").css({
                width: $west.width(),
                height: $west.height() - 37
            });


            var $center = $("#div_layout").layout("panel", "center");
            // $('#tt_tabs').tabs('resize',{
            //     width: $(window).width()- $west.width()-7,
            //     height: $center.height()
            // });



            //$('#tt_menu').yqTree("resize", {
            //    width: $(window).width(),
            //    height:$(window).height() - 24
            //});

            if (!(setResizeTime === undefined)) {
                clearInterval(setResizeTime);
                setResizeTime = undefined;
            };
        }

        $(function () {
            $.yqDbList({ ajaxUrl: "_db.html" },
                function (data) {
                    if (data.error == 0) {
                        dbname = data.rows[0].name;
                        console.log(data, dbname);
                        init_cms();
                    } else {
                        alert(data.info);
                    }
                });
        });

        function init_cms() {
            $("#cg_cms").yqComboGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_cms",
                keyId: "sid",
                displayMember: "name",

                titles: [],
                loadData: false,
                isEdit: true,
                isSeek: false,

                isPage: false,

                move: true,

                editMode: 2,
                editType: 2,    //2:form编辑模式
                editModeW: 1048,
                editModeH: 564,
                editModeNo: 2,

                uppActionState: function (value, row, callback) {
                    callback(true);
                },
                afterBindingTitle: function () {
                    init_navigation();
                },
                onChange: function (newValue, newRow, oldValue, oldRow) {
                    if (newRow === null) {
                        $tree_nav.tree('loadData', []);
                        $("#tt_site_info").yqFormEdit("setRow", {});
                    } else {
                        if (newRow['navigation'] === undefined) {
                            $tree_nav.tree('loadData', []);
                        } else {
                            //console.log(newRow['navigation']);
                            var res_nodes = $.yqTreeloadInit(newRow['navigation'], 'name');
                            $tree_nav.tree('loadData', res_nodes.data);
                        }
                        $("#tt_site_info").yqFormEdit("setRow", newRow);
                    }

                    $("#tt_navigation_info").yqFormEdit("setRow", {});
                },
            });
        }

        var $tree_nav;
        function init_navigation() {
            $tree_nav = $('<ul></ul>');
            $("#tt_navigation").append($tree_nav);
            $tree_nav.tree({
                onClick: function (node) {
                    //console.log(node);
                    $("#tt_navigation_info").yqFormEdit("setRow", node);
                }
            });

            //获取模板
            var opt = $("#cg_cms").yqComboGrid("options");
            var pp_opt = {
                ajaxUrl: opt.ajaxUrl,
                dbname: opt.dbname,                //数据库
                tbname: opt.tbname,                //表
                tempno: 3
            };
            $.yqTbModel(pp_opt, function (res) {
                if (res.error == 0) {
                    init_site_info(opt.titles, res.model)
                } else {
                    init_site_info(opt.titles, JSON.stringify(opt.titles));
                }
            })
        }

        function init_site_info(titles, model) {
            //console.log("titles",titles);
            $("#tt_site_info").yqFormEdit({
                ajaxUrl: "/_db.html",
                titles: titles,
                model: model,
                row: {},
                loadDefault: false,   //加载默认值
                controlType: 2,        //2-div模式
                isEdit: false,
                blankTag: "*",     //值不能为空，标题附加标记
                afterBindingTitle: function () {
                    var nav_ui_mask = {};
                    for (var i = 0; i < titles.length; i++) {
                        if (titles[i].name == "navigation") {
                            nav_ui_mask = titles[i].ui_mask;
                            break;
                        }
                    }

                    var model = null;
                    for (var i = 0; i < nav_ui_mask.param.model.length; i++) {
                        if (nav_ui_mask.param.model[i].no == 1) {
                            model = nav_ui_mask.param.model[i].model;
                            break;
                        }
                    }

                    init_navigation_info(nav_ui_mask.param.titles, model);
                },   //标题绑定后
            });
        }

        function init_navigation_info(titles, model) {
            //console.log("titles",titles);
            $("#tt_navigation_info").yqFormEdit({
                ajaxUrl: "/_db.html",
                titles: titles,
                model: model,
                row: {},
                loadDefault: false,   //加载默认值
                controlType: 2,        //2-div模式
                isEdit: false,
                blankTag: "*",     //值不能为空，标题附加标记
                afterBindingTitle: function () {
                    form_load();
                },   //标题绑定后
            });
        }

        //tt_site_info
    </script>
</body>

</html>